package net.sansi.v3correctserver.entity.image;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import net.sansi.v3correctserver.entity.IntPoint;
import net.sansi.v3correctserver.entity.color.CIEChroma;
import net.sansi.v3correctserver.entity.color.PhotoColor;

import java.awt.*;
import java.nio.file.Path;

/**
 * Created by Fang
 * create time : 2024/11/5
 * IDE : IntelliJ IDEA
 */

@Slf4j
public abstract class RecImageBase implements RecImage {

    // 图片路径
    @Setter
    private Path path;

    /**
     * 将路径转换为绝对路径
     *
     * @return 绝对路径
     */
    public Path getPath() {
        return path.toAbsolutePath();
    }

    /**
     * 获取颜色
     *
     * @param x x坐标
     * @param y y坐标
     * @return 颜色
     */
    @Override
    public Color getColor(int x, int y) {
        return getPhotoColor(x, y).toColor();
    }

    /**
     * 获取 cie 颜色
     *
     * @param x x 坐标
     * @param y y 坐标
     * @return cie 坐标
     */
    @Override
    public CIEChroma getCIEChroma(int x, int y) {
        return getPhotoColor(x, y).toCIEChroma();
    }

    @Override
    public double getBrightness(int x, int y) {
        return getPhotoColor(x, y).getBrightness();
    }

    @Override
    public void save(Path path) {}


    @Override
    public void setColor(int x, int y, PhotoColor color) {}

    @Override
    public void setColor(IntPoint point, PhotoColor color) {
        setColor(point.getX(), point.getY(), color);
    }


    @Override
    public void drawLine(IntPoint p1, IntPoint p2, PhotoColor color) {
        if (p1.getX() > p2.getX()) {
            IntPoint tmp = p1;
            p1 = p2;
            p2 = tmp;
        }
        int x1 = p1.getX(), x2 = p2.getX();
        int y1 = p1.getY(), y2 = p2.getY();
        int lastY = y1;
        double ratio = 0;
        if (x2 - x1 > 0) ratio = (y2 - y1) * 1.0 / (x2 - x1);
        for (int x = x1; x <= x2; x++) {
            int y = y2;
            if (x < x2) y = (int) Math.round(ratio * (x - x1) + y1);
            int center = x1 == x2 ? 0 : (lastY + y) >> 1;
            setColor(x, y, color);
            while (ratio >= 0 ? (lastY < y) : (lastY > y)) {
                boolean flag = ratio >= 0 ? (lastY >= center) : (lastY <= center);
                setColor(flag ? x : x - 1, lastY, color);
                lastY += ratio >= 0 ? 1 : -1;
            }
        }
    }

    @Override
    public void drawRect(IntPoint ltPoint, IntPoint rbPoint, PhotoColor color) {
        IntPoint rtPoint = new IntPoint(rbPoint.getX(), ltPoint.getY());
        IntPoint lbPoint = new IntPoint(ltPoint.getX(), rbPoint.getY());
        drawLine(ltPoint, rtPoint, color);
        drawLine(rtPoint, rbPoint, color);
        drawLine(ltPoint, lbPoint, color);
        drawLine(lbPoint, rbPoint, color);
    }

    @Override
    public void fillRect(IntPoint ltPoint, IntPoint rbPoint, PhotoColor color) {
        int x1 = ltPoint.getX(), x2 = rbPoint.getX();
        int y1 = ltPoint.getY(), y2 = rbPoint.getY();
        for (int x = x1; x <= x2; x++) {
            for (int y = y1; y <= y2; y++) {
                setColor(x, y, color);
            }
        }
    }

    @Override
    public void writeText(IntPoint startPoint, String text, PhotoColor color) {}

    @Override
    public void setBackgroundColor(PhotoColor color) {
        fillRect(new IntPoint(0, 0), new IntPoint(getWidth() - 1, getHeight() - 1), color);
    }

    /**
     * 获取图片路径字符串
     * 将PATH 转换为 UTF8 路径
     *
     * @param path 图片路径
     * @return 图片路径字符串
     */
    public String path2String(Path path) {
        return path.toAbsolutePath().toString();
    }
}
